import math

def validpeak(sample_sign, s2limit, peak, wavelength, lattconst):
    dspacing=math.sqrt(lattconst**2/(peak[0]**2+peak[1]**2+peak[2]**2))
    qval=2*3.14159265/dspacing
    #print qval
    # Note: we'll use 0.12 as the limit in q needed to reach the upper limit
    if wavelength < 4*3.14159265/(qval+0.12):
        qmin=qval-0.12
        qmax=qval+0.12
        #tth=2*180.0*math.asin(wavelength/(2*dspacing))/3.14159265
        tthmin=2*180.0*math.asin(qmin*wavelength/(4*3.14159265))/3.14159265
        tthmax=2*180.0*math.asin(qmax*wavelength/(4*3.14159265))/3.14159265
        tthmin=round(tthmin,2)
        tthmax=round(tthmax,2)
        s2comp=abs(s2limit)
        if tthmax > s2comp:
            isvalid=0
        else:
            tthmin=sample_sign*tthmin
            tthmax=sample_sign*tthmax
            isvalid=1
    else:
        tthmin=-1.00
        tthmax=-1.00
        isvalid=0
    return isvalid, tthmin, tthmax